const app = getApp()
const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
const recorderManager = wx.getRecorderManager();
var recordInterval = null;
var timeCountDown = null;
Page({
  data: {
    previewOpenid: '',// 被查看的用户openid
    previewType: 0,// 浏览类型：1自己查看得分  2圈主查看用户的得分  3圈主预览
    themeid: "",// 主题id
    auxiliary: { "viewType": 0, "now": util.formatDate(new Date()), 'openid': app.globalData.logininfo.openid, diaryInfoPageNum: 1, diaryInfoPageSize: 20, isGetAllDiary: true},//viewType测评考试的显示类型 0 全部题目 1错题解析
    answerInfo: {},// 数据库的数据
    //圈子日记数据
    diaryInfoList:[],
  },

  onLoad: function (options) {
    var _this = this;
    _this.setData({
      themeid: options.themeid,
      previewType: options.previewType,
      previewOpenid: options.previewOpenid == null ? "" : options.previewOpenid
    })
    _this.getAnswerList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    var isGetAllDiary = _this.data.auxiliary.isGetAllDiary;
    if (isGetAllDiary){
      _this.setData({
        ["auxiliary.diaryInfoPageNum"]: _this.data.auxiliary.diaryInfoPageNum + 1
      })
      _this.getDiaryInfo();
    }
  },

  /**
   * 获取测评、问卷的回答信息
   */
  getAnswerList: function () {
    var _this = this;
    var themeid = _this.data.themeid;
    var openid = app.globalData.logininfo.openid;
    var previewType = _this.data.previewType;
    var previewOpenid = _this.data.previewOpenid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.themeid = themeid;
        data.previewType = previewType;
        data.previewOpenid = previewOpenid;
        data.access_token = res.access_token;
        util.post(api.Interest_GetAnswerListApi, data).then(res => {
          if (res.code == 200) {
            var answerInfo = res.result;
            var dataInfoList = answerInfo.dataInfoList;
            for (var i = 0; i < dataInfoList.length; i++) {
              var dataInfo = dataInfoList[i];
              switch (dataInfo.type) {
                case 3:
                  dataInfo.startDate = "00:00";
                  dataInfo.voiceBoFangImage = api.AudioNoBoFangImage_Name;
                  dataInfo.currentTime = 0;
                  break;
                case 4:
                  var videoid = "myVideo" + new Date().getTime() + i;
                  dataInfo.videoid = videoid;
                  break;
              }
              dataInfoList[i] = dataInfo;
            }
            answerInfo.dataInfoList = dataInfoList;
            _this.setData({
              answerInfo: answerInfo
            })
            _this.getDiaryInfo();
          }
        })
      })
    }
  },

  /**
   * 测试类型结果展示类型
   */
  selectViewType:function(e){
    var _this = this;
    var index = e.currentTarget.dataset.index;
    _this.setData({
      ["auxiliary.viewType"]: index,
    })
  },

  /**
   * 获取日记详情
   */
  getDiaryInfo: function () {
    var _this = this;
    var themeid = _this.data.themeid;
    var circleid = _this.data.answerInfo.ordTheme.circleid;
    var diaryInfoPageNum = _this.data.auxiliary.diaryInfoPageNum;
    var diaryInfoPageSize = _this.data.auxiliary.diaryInfoPageSize;
    var openid = app.globalData.logininfo.openid;
    var data = {};
    data.themeid = themeid;
    data.pageNum = diaryInfoPageNum;
    data.pageSize = diaryInfoPageSize;
    data.circleid = circleid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.getDiaryInfoQuery(api.Home_GetDiaryInfoApiUrl, data);
      })
    } else {
      _this.getDiaryInfoQuery(api.Home_GetDiaryInfoUrl, data);
    }
  },

  /**
   * 获取日记详情：请求
   */
  getDiaryInfoQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      if (res.code == 200) {
        var resultList = res.result.list;
        var list = _this.data.diaryInfoList;
        for (var i = 0; i < resultList.length; i++) {
          list.push(resultList[i]);
        }
        _this.setData({
          diaryInfoList: list,
          ["auxiliary.isGetAllDiary"]: resultList.length > 0 ? true : false
        })
      }
    });
  },

  /**
   * 跳转成绩排行榜
   */
  gotoThemeRanking: function () {
    var _this = this;
    var themeid = _this.data.answerInfo.ordTheme.id;
    wx.navigateTo({
      url: '../themeRanking/themeRanking?themeid=' + themeid,
    })
  },

  /**
   * 跳转调查统计
   */
  gotoTSurveyStatistics: function () {
    var _this = this;
    var themeid = _this.data.answerInfo.ordTheme.id;
    wx.navigateTo({
      url: '../surveyStatistics/surveyStatistics?themeid=' + themeid,
    })
  },

  /**
   * 进入用户主页
   */
  navigateToPersonhome: function (e) {
    var openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url: '../../../../../../ucenter/pages/ucenter/personhome/personhome?openid=' + openid
    })
  },

  /**
   * 预览图片：日记
   */
  previewImage: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var yindex = e.currentTarget.dataset.yindex;
    var themeImageList = _this.data.answerInfo.dataInfoList[index].themeImageList;
    var urls = [];
    for (var i = 0; i < themeImageList.length; i++) {
      urls.push(themeImageList[i].ip + themeImageList[i].path);
    }
    var current = themeImageList[yindex].ip + themeImageList[yindex].path;
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  
  /**
  * 跳转到公众号展示页面：日记
  */
  navigateToWebView: function (e) {
    var ip = e.currentTarget.dataset.ip;
    var text = e.currentTarget.dataset.text;
    var type = e.currentTarget.dataset.type;
    if (type == 1) {
      wx.navigateTo({
        url: '../../../../../../pages/home/webView/webView?src=' + text,
      })
    } else {
      wx.downloadFile({
        url: ip + text,
        success: function (res) {
          var filePath = res.tempFilePath;
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
            }
          })
        }
      })
    }
  },

  /**
   * 下一个主题
   */
  nextTurnTheme: function(){
    var _this = this;
    var nextTurnThemeType = _this.data.answerInfo.nextTurnThemeType;
    if (nextTurnThemeType == 0) {
      return;
    }
    var themeid = _this.data.answerInfo.nextTheme.id;
    if (nextTurnThemeType == 1) {
      // 跳转到回答页
      wx.redirectTo({
        url: '../../themeDetails/themeDetails?themeid=' + themeid + '&preview=0',
      })
    } else if (nextTurnThemeType == 2) {
      //跳转到答案页
      wx.redirectTo({
        url: 'themeResult?themeid=' + themeid + '&previewType=' + _this.data.previewType,
      })
    } else if (nextTurnThemeType == 3) {
      // 跳转到宝贝详情页面
      wx.redirectTo({
        url: '../../goodsDetails/goodsDetails?themeid=' + themeid,
      })
    }
  },

  /**
   * 上一个主题
   */
  lastTurnTheme: function () {
    var _this = this;
    var lastTurnThemeType = _this.data.answerInfo.lastTurnThemeType;
    if (lastTurnThemeType == 0){
      return;
    }
    var themeid = _this.data.answerInfo.lastTheme.id;
    if (lastTurnThemeType == 1){
      // 跳转到回答页
      wx.redirectTo({
        url: '../../themeDetails/themeDetails?themeid=' + themeid + '&preview=0',
      })
    } else if (lastTurnThemeType == 2){
      //跳转到答案页
      wx.redirectTo({
        url: 'themeResult?themeid=' + themeid + '&previewType=' + _this.data.previewType,
      })
    } else if (lastTurnThemeType == 3){
      // 跳转到宝贝详情页面
      wx.redirectTo({
        url: '../../goodsDetails/goodsDetails?themeid=' + themeid,
      })
    }
  },

  /**
   * 目录 
   */
  directory: function(){
    var _this = this;
    wx.redirectTo({
      url: '../../../../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?poTopViewIndex=1&circleid='+_this.data.answerInfo.ordTheme.circleid,
    })
  },

  /**
   * 去打卡
   */
  doClock: function(){
    var _this = this;
    var themeid = _this.data.answerInfo.ordTheme.id;
    var themeContentType = _this.data.answerInfo.ordTheme.themeContentType;
    var circleid = _this.data.answerInfo.ordTheme.circleid;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.circleid = circleid;
        data.themeid = themeid;
        data.access_token = res.access_token;
        util.post(api.Interest_CheckUserThemeClockApi, data).then(function (res) {
          if (res.code == 200) {
            var result = res.result;
            var type = result.type;
            var message = result.message;
            if (type == 1) {
              //可以进行打卡 
              wx.navigateTo({
                url: '../../../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/noThemeClock/noThemeClock?themeid=' + themeid + '&circleid=' + circleid,
              })
            } else if (type == 2) {
              //不可以进行打卡
              util.showToast(message, "none");
            } else if (type == 4) {
              //需要先进行回答主题
              if (themeContentType == 4 || themeContentType == 5) {
                // 宝贝
                wx.redirectTo({
                  url: '../../goodsDetails/goodsDetails?themeid=' + themeid,
                })
              } else if (themeContentType == 1) {
                // 知识分享，进入打卡页面
                wx.navigateTo({
                  url: '../../../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/noThemeClock/noThemeClock?themeid=' + themeid + '&circleid=' + circleid,
                })
              } else {
                wx.redirectTo({
                  url: '../../themeDetails/themeDetails?themeid=' + themeid + '&preview=0',
                })
              }
            }
          } else {
            util.showToast(res.message, "none");
          }
        })
      })
    }
  },
})